/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg_group5;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

import java.util.ArrayList;

/**
 *
 * @author Matt Carr.
 * 
 */
@WebService()
public class WishLists {

    //Stores a bookstore into the database
    //Makes it so multiple bookstores can use the same service but only access
    //their data.
    @WebMethod(operationName = "RegisterBookstore")
    public int registerBookstore(
            @WebParam(name = "login") String login,
            @WebParam(name = "password") String password) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int register = db.registerBookstore(login, password);

        if (register == 0) {
            return 0; //Success.
        } else if (register == 1) {
            return 1; //Bookstore already exists
        } else if (register == -1) {
            return -1; //Unexpected Database Error
        }

        return -1; //Unexpected Error.
    }

    //Adds item to a wishlist. If a Wishlist doesn't exist for the userID specified
    //then one is created automatically. Stores the item in the database
    //for maintainability, only added if the item doesn't already exist.
    @WebMethod(operationName = "AddItemToWishlist")
    public int addItemToWishList(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword,
            @WebParam(name = "UserID") int userID,
            @WebParam(name = "ItemID") int itemID) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            db.addItem(bookstoreID, itemID);

            if (db.addItemToWishlist(userID, itemID, bookstoreID) == 0) {
                return 0; //SUCCESS
            } else {
                return 1; //Item Already In Wishlist
            }
        } else if (bookstoreID == -1) {
            return -1; //Unexpected Database Error
        } else if (bookstoreID == -2) {
            return -2; //Failed Authentication
        }

        return -1; //Unexpected Error
    }

    //Allows bookstore to remove a reference to an item. Removes from any
    //wish lists.
    @WebMethod(operationName = "DeleteItem")
    public int deleteItem(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword,
            @WebParam(name = "itemID") int itemID) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            if (db.deleteItem(bookstoreID, itemID) == 0) {
                return 0; //Item removed from wishlist
            } else {
                return 1; //Error removing item
            }
        } else if (bookstoreID == -1) {
            return -1; //Unexpected Database Error
        } else if (bookstoreID == -2) {
            return -2; //Failed Authentication
        }

        return -1; //Unexpected Error
    }

    //Self explanatory.
    @WebMethod(operationName = "RemoveItemFromWishlist")
    public int removeItemFromWishList(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword,
            @WebParam(name = "userID") int userID,
            @WebParam(name = "itemID") int itemID) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            if (db.removeItemFromWishlist(userID, itemID, bookstoreID) == 0) {
                return 0; //Item removed from wishlist
            } else {
                return 1; //Error removing item
            }
        } else if (bookstoreID == -1) {
            return -1; //Unexpected Database Error
        } else if (bookstoreID == -2) {
            return -2; //Failed Authentication
        }

        return -1; //Unexpected Error
    }

    @WebMethod(operationName = "RemoveMultipleItemsFromWishlist")
    public int removeMultipleItemsFromWishList(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword,
            @WebParam(name = "userID") int userID,
            @WebParam(name = "itemIDs") int[] items) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            boolean errorOccurred = false;

            //Loop through and remove items
            for (int i = 0; i < items.length; i++) {
                if (db.removeItemFromWishlist(userID, items[i], bookstoreID) != 0) {
                    errorOccurred = true;
                }
            }

            if (errorOccurred == true) {
                return 1; //One or more items may not have been removed successfully.
            } else {
                return 0;
            }
        } else if (bookstoreID == -1) {
            return -1; //Unexpected Database Error
        } else if (bookstoreID == -2) {
            return -2; //Failed Authentication
        }

        return -1; //Unexpected Error
    }

    //Returns an ArrayList of itemIDs. Bookstore should be able to use this
    //and return information from their own database holding the items.
    @WebMethod(operationName = "GetWishlist")
    public ArrayList getWishList(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword,
            @WebParam(name = "userID") int userID) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            return db.getItemsInWishlist(userID, bookstoreID);
        }

        return null; //Error occurred
    }

    //Returns the most popular item (appears within the most wishlists)
    //within a given bookstore.
    @WebMethod(operationName = "GetMostWantedItem")
    public int getMostWantedItem(
            @WebParam(name = "BookstoreName") String bookstoreName,
            @WebParam(name = "BookstorePassword") String bookstorePassword) {

        WishListDatabaseFunctions db = new WishListDatabaseFunctions();

        int bookstoreID = db.connectBookstore(bookstoreName, bookstorePassword);

        if (bookstoreID >= 0) {
            return db.getMostWantedItem(bookstoreID); //Success: Returns id of item.
        } else if (bookstoreID == -1) {
            return -1; //Unexpected Database Error
        } else if (bookstoreID == -2) {
            return -2; //Failed Authentication
        }

        return -1; //Unexpected Error
    }
}
